package presentation;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ImageServlet
 */
@WebServlet("/ImageServlet")
public class ImageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ImageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

    	String imagePath =  request.getPathInfo();  // request.getParameter("imagePath");
    	String filePath = getServletContext().getInitParameter("file-upload");
    	
    	if (!filePath.trim().endsWith("\\"))
    		filePath = filePath+ "\\";
    	
    	response.setContentType("image/jpeg");

    	String pathToWeb = filePath + imagePath;
    	File f = new File(pathToWeb);
    	BufferedImage bi = ImageIO.read(f);
    	OutputStream out = response.getOutputStream();
    	ImageIO.write(bi, imagePath.substring(imagePath.lastIndexOf(".") +1), out);
    	out.close();

    }


}






